package com.shigen.demo.controller;

import com.shigen.demo.bean.Result;
import com.shigen.demo.entity.ProjectData;
import com.shigen.demo.entity.req.ProjectDataReq.QueryItem;
import com.shigen.demo.service.IProjectDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "projectdata")
@Api(tags = "表单数据的处理")
public class ProjectDataController {

  @Autowired
  private IProjectDataService projectDataService;

  @PostMapping(value = "query")
  @ApiOperation(value = "根据表单的id分页查询数据")
  public Result getProjectData(@RequestBody @Valid QueryItem queryItem) {
    return projectDataService.getProjectData(queryItem);
  }

  @PostMapping(value = "/insert")
  @ApiOperation(value = "添加一条表单数据")
  public Result insert(@RequestBody ProjectData projectData, HttpServletRequest request) {
    return projectDataService.addProjectData(projectData, request);
  }

  @DeleteMapping(value = "/delete")
  @ApiOperation(value = "根据提交数据的id批量删除数据")
  public Result<String> deleteBatch(@RequestBody List<Integer> ids) {
    int delete = projectDataService.deleteBatchIds(ids);
    return delete > 0 ? Result.ok("已成功删除 " + delete + " 条数据") : Result.err("删除失败");
  }

  @PostMapping(value = "update")
  @ApiOperation(value = "更新数据")
  public Result<String> update(@RequestBody ProjectData projectData) {
    int update = projectDataService.update(projectData);
    return update > 0 ? Result.ok("更新成功") : Result.err("更新失败");
  }
}
